package com.ycxy.springsecuritycourse.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class LoginUser implements UserDetails {

    //原有用户信息
    private Users users;

    //权限信息
    private List<String> perssionList;

    @JSONField(serialize = false)
    private List<SimpleGrantedAuthority> authorities;
    public LoginUser(){
    }
    public LoginUser(Users users, List<String> perssionList){
        this.users=users;
        this.perssionList=perssionList;
    }

    public void setUsers(Users users) {
        this.users = users;
    }

    public void setPerssionList(List<String> perssionList) {
        this.perssionList = perssionList;
    }

    public List<String> getPerssionList() {
        return perssionList;
    }

    public Users getUsers() {
        return users;
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(authorities!=null){
            return authorities;
        }
        authorities = new ArrayList<>();
        for (String perssion : perssionList) {
            authorities.add(new SimpleGrantedAuthority(perssion));
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return users.getPassword();
    }

    @Override
    public String getUsername() {
        return users.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
